# import student
from student import student
from datetime import datetime

class studentCms(object):
    def __init__(self):
        # 用于存储各个学生对象
        self.list = []

    def add_stu(self):
        id = input('请输入学生ID: ')
        name = input('请输入学生姓名: ')
        tel = input('请输入学生手机号: ')
        stu = student(id,name,tel)
        self.list.append(stu)

    def del_stu(self):
        del_id = input('请输入您要删除的学生编号：')
        for stu in self.list:
            if stu.id == del_id:
                self.list.remove(stu)
                break

    def modify_stu(self):
        modify_id = input('请输入您要修改的学生编号：')
        modify_name = input('是否需要修改姓名？是，则输入：true,姓名 否则输入：false。例如：true,张三：').split(',')
        modify_tel = input('是否需要修改手机号？是，则输入：true,手机号 否则输入：false。例如：true,173：').split(',')
        
        for stu in self.list:
            if stu.id == modify_id:
                if modify_name[0] == 'true':
                    stu.name = modify_name[1]
                if modify_tel[0] == 'true':
                    stu.tel = modify_tel[1]
                break

    def show_single_stu(self):
        flag = 0
        name = input('请输入要查询的学生姓名: ')
        for stu in self.list:
            if stu.name == name:
                print(stu)
                flag = 1
        if flag == 0:
            print('未找到查询结果')
        

    def show_all_stu(self):
        for stu in self.list:
            print(stu)

    def save(self):
        with open('studentInfo.txt','a',encoding='utf-8') as f:
            f.write(str(datetime.now().strftime('%Y/%m/%d %H:%M:%S') + '\n'))
            for stu in self.list:
                f.write(str(stu) + '\n')
        print('文件已保存')

    def start(self):
        index_info = '*'* 20  + '\n1. 添加学生\n2. 删除学生\n3. 修改学生信息\n4. 查询单个学生信息\n5. 查询所有学生信息\n6. 保存信息\n7. 退出系统\n' + '*'* 20
        while True:
            print(index_info)
            num = input('请输入您要操作的编号：')
            if num == '1':
                self.add_stu()
            elif num == '2':
                self.del_stu()
            elif num == '3':
                self.modify_stu()
            elif num == '4':
                self.show_single_stu()
            elif num == '5':
                self.show_all_stu()
            elif num == '6':
                self.save()
            elif num == '7':
                flag = input('是否要退出[y/n]： ')
                if flag == 'y':
                    print('\n系统已退出！')
                    break
                else:
                    continue
            else:
                print('请输入0~6内的数字！')

if __name__ == '__main__':
    stuCms = studentCms()
    stuCms.start()